space<template>
  <div class="container">
    <div class="app-container">
      <!-- 展示树形结构 -->
      <el-tree :expand-on-click-node="false" :data="data" :props="defaultProps" default-expand-all>
        <!-- 节点结构 -->
        <!-- flex，justify:space-between：左右均分，align:center：居中 -->
        <template v-slot="{ data }">
          <el-row style='width:100%;height:40px;' type="flex" justify="space-between" align="center">
            <el-col style="height: 40px; line-height: 40px;">{{ data.name }}</el-col>
            <!-- :span="4"：占4格 必须加‘ ：’ -->
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- 下拉菜单 -->
              <el-dropdown @command="operateDept($event, data.id)">

                <span class="el-dropdown-link" style="display: inline-block;width: 60px; margin-left: 40px; ">
                  操作<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>

      </el-tree>
    </div>
    <!-- 放置弹层 -->
    <!-- 表示会接受子组件的事件， update:showDialog的值 -->
    <add-dept ref="addDept" :currentNodeId="currentNodeId" :showDialog.sync='showDialog'
      :updateDepartment="getDepartments"></add-dept>
  </div>
</template>
<script>
import { deleteDepartment, getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
import addDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    addDept
  },
  data() {
    return {
      currentNodeId: null,
      showDialog: false, // 控制弹出层显示和隐藏
      data: [],// 数据属性
      defaultProps: {
        children: 'children',  // 读取子节点的字段名
        label: 'name' //要显示字段的名字
      }
    }
  },
  created() {
    this.getDepartments()

  },
  methods: {
    async getDepartments() {
      const result = await getDepartment()
      this.data = transListToTreeData(result, 0)


      

    },
    operateDept(type, id) {
      if (type === 'add') {
        // 添加子部门
        this.showDialog = true
        this.currentNodeId = id
      } else if (type === 'edit') {
        //编辑部门
        this.showDialog = true
        this.currentNodeId = id

        // this.$nextTick() 确保数据更新后再执行

        this.$nextTick(() => {
          // 要在子组件获取数据
          // 父组件调用子组件的方法
          const addDept = this.$refs.addDept // this.$refs.appDept 相当于子组件的this
          addDept.getDepartmentDetail()
          
        })
      } else {
        // 删除部门
        this.$confirm('是否删除该部门？','提示').then(async ()=>{
          await deleteDepartment(id)
          this.$message.success('删除成功')
          this.getDepartments()
        }).catch(()=>{
          this.$message.info('取消删除')

        })
      }



    }
  }
}
</script>
<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}

.tree-manager {

  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>